
--[[--
- @Copyright (C), 2016-, sixiong.
- @Name user.lua
- @Author sixiong
- @Version 1.0
- @Date: 2016年6月11日下午22:06:41
- @Description 文章模型层
- @Class
- @Function List
- @History <author> <time> <version > <desc>
            sixiong 2016年6月11日下午22:06:41  1.0  第一次建立该文件
--]] 
local user = model.__new( _CONF["mysql"] , "mysql");

--[[
- @desc   获取文章条数 
--]]
user.table = 'mg_user_member';

--[[
- @desc   获取文章条数
- @param  int    id  字符串
- return  table
--]]
function user:count(where) 
     local sql = "SELECT COUNT(DISTINCT uid) AS __count FROM `".. self.table .."` WHERE `visible` =1"; 
     local res, err, errno, sqlstate = self.mysql(sql);
     return res;
end
 


--[[
- @desc   获取一条文章
- @param  int    id  字符串
- return  table
--]]
function user:get(option) 
     if not option then return nil; end
     local wheres = self:handle(option); 
     local sql = "SELECT * FROM " .. self.table .. " WHERE " .. wheres .. " limit 1"; 
     local res, err, errno, sqlstate = self.mysql(sql, 1); 
     return res;
end
 

--[[
- @desc   验证密码
- @param  int       mobile  手机号
- @param  string    passwd  密码
- return  boole
--]]
function user:checkpasswd(mobile, passwd)
     if not mobile or not passwd then  
         return nil;
     end
     
     local userInfo = self:get({mobile=mobile});
     if  not userInfo then 
         return nil;
     end
     
     local  passwd = self:dePassword(passwd, userInfo[1]["passkey"]);
      
     if  userInfo[1]["password"] ~= passwd then
          return nil;
     end 
     return userInfo[1];
end

  
  
--[[
- @desc   获取一条 
- @param  int    id  字符串
- return  table
--]]
function user:getone(uid, passkey)
     if not uid then
         dump('uid错误');
     end
     
     local sql = "SELECT * FROM " .. self.table .. " WHERE uid = " .. tonumber(uid) .. " AND passkey = " .. passkey .. " limit 1";
     local res, err, errno, sqlstate = self.mysql(sql);
     return res;
end


--[[
- @desc   获取多条文章
- @param  table where   字符串
- @param  string limit   字符串
- return  table
--]]
function user:getall(where, limit)
     if limit then 
		 limit = " LIMIT  limit";
	 end
 
     local sql = 'SELECT * FROM  ' .. self.table .. limit;  
     local res, err, errno, sqlstate = self.mysql(sql);
     return res;
end 


--[[
- @desc   添加
- @param  table   option 数据
- return  int   自增id
--]]
function user:add(mobile, nickname, passwd)
     local data = {};
     if not data then
        return nil;
     end

     if passwd then 
         data['passkey'], data['password'] = self:enPassword(passwd);
     end
     
     data['nickname'] = nickname;
     data['mobile'] = mobile;
     data['regtime'] = _TIMESTAMP;    
       
     local key = "";
     local val = ""; 
     for k, v in pairs(data) do  
        if not v then
            table.remove(data, k);
        else
              key = key .. '`'.. k ..'`,';
              val = val .. '"'.. v .. '",';   
        end
     end 
       
     key = key .. "`logintime`";
     val = val .. '"' .. _TIMESTAMP ..'"'; 
     
     local sql =  'insert into ' .. self.table .. '('.. key ..')values('.. val ..')';
 
	 local res, err, errno, sqlstate = self.mysql(sql); 
	  --[[ res结果
		    [insert_id] : 1188
			[server_status] : 2
			[warning_count] : 0
			[affected_rows] : 1,
	  --]] 
	 return res['insert_id'];
end


--[[
- @desc   修改一条文章 
- @param  int     id  
- @param  table   option 数据
- return   int    影响行数
--]]
function user:update(id, option)
	 local res, err, errno, sqlstate = self.mysql("UPDATE " .. self.table .. " SET `status` = 0 WHERE `item_comment`.`id` = '67'"); 
	 return res;
end


--[[
- @desc   删除
- @param  mixed  id 数据
- return   int    影响行数
--]]
function user:del(id)
	 local res, err, errno, sqlstate = self.mysql("DELETE FROM ".. self.table .. " WHERE id = 1"); 
	 return res;
end
 
 
--[[
 * @desc 生成密码
 * @access public
 * @param string $password 密码明文
 * @return string
--]]
 function  user:enPassword(password)
      math.randomseed(tostring(os.time() .. (os.clock())*100 ):reverse():sub(1, 8)); 
      local passKey =  math.random(100000, 999999);
      local pwd = ngx.md5(password .. passKey);
      return passKey , pwd;
 end
 
 
--[[
 * @desc 解密密码
 * @access public
 * @param string $password 密码明文
 * @param string $passKey  随机key
 * @return string
--]]
 function  user:dePassword(password, passKey)
	  return  ngx.md5(password .. passKey); 
 end
 
 
 
--[[
- @desc   sql条件处理条件
- @param  table    where  条件
- return  string
--]]
function user:handle(where)
     local str = "";
     if not where then 
          return sql;
     end
     
     if where["id"] ~= nil and where["id"] ~= "" then
       str = str ..' id = ' .. tonumber(where["id"]) .. ' AND ';
     end
      
     if where["passkey"] ~= nil and where["passkey"] ~= "" then
       str = str ..' passkey = ' .. where["passkey"] .. ' AND ';
     end
      
     if where["mobile"] ~= nil and where["mobile"] ~= ""  then
       str = str ..' mobile = ' .. where["mobile"]  .. ' AND ';
     end
 
     str = str ..' 1=1 ';
     return str;
end

return user;